package net.uman.framework.modules.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class SqlHelper {
	private static Properties props = new Properties();
	
	static {
		InputStream is = SqlHelper.class.getResourceAsStream("/sql.properties");
		try {
			props.load(new InputStreamReader(is, "UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static String getSql(String name) {
		if(name == null){
			throw new IllegalArgumentException("Name needed");
		}
		
		String s = props.getProperty(name);
		if (s.startsWith("@SQL@") || s.startsWith("@HQL@")) {
			s = s.substring(5);
		}
		return s;
	}
	
	public static boolean isNativeSql(String name) {
		if(name == null){
			throw new IllegalArgumentException("Name needed");
		}
		
		String s = props.getProperty(name);
		return s.startsWith("@SQL@");		
	}
	
	public static void main(String[] args) {
		String s = SqlHelper.getSql("sql1");
		System.out.println("s:"+s);
		s = SqlHelper.getSql("sql2");
		System.out.println("s:"+s);
	}
}
